/**
 *
 * 只依赖于requirejs
 *
 * 参数说明
 * obj 为原生对象或者id
 * opts 为参数
 *
 */

define([
    '../DateBox/extend.js',
    '../DateBox/DateTime.js',
    '../DateBox/DateComponent.js',
    '../DateBox/hasEle.js',
    '../DateBox/clearFix.js',
    '../DateBox/defaultOpts.js'
],function(
    extend,
    DateTime,
    DateComponent,
    hasEle,
    clearFix,
    defaultOpts
){

    var DateBox = function(obj,opts,stateTime,endTime,num){


        this.num = num;
        this.objs = obj;
        this.sizeOptis = {
            width:140,
            height:140,
            minDatas:stateTime,
            maxDatas:endTime,
            mindata:stateTime,
            maxdata:endTime
        }

        //定义时间参数
        //容器
        this.obj = document.getElementById(obj)?document.getElementById(obj):obj;
        //参数
        this.opts = extend(opts||{},defaultOpts);
        //默认日期
        this.dateTime = new DateTime(this.opts.date||{});

        var datms = stateTime.split('-');

        this.dateTime.year = parseInt(datms[0]);

        if(num == 1 || num == 2){
            this.dateTime.month = parseInt(this.Detail());
        }else{
            this.dateTime.month = parseInt(datms[1]);
        }

        this.dateTime.date = parseInt(datms[2]);


        var rightPosition  = parseInt(this.obj.style.width.replace('px',''));
        this.rightPosition = this.sizeOptis.width-rightPosition;

        this.initDOM();						//初始化dom
        this.initStyle();					//初始化样式
        this.addEL();						//添加事件
        if(num == 3){
            this.addComponent();				//添加组成部分
            this.initCS();						//初始化组建状态
            this.timeChangeCallBack();			//添加时间更改后的回掉函数
        }
    };
    //初始化DOM元素
    DateBox.prototype.initDOM = function(){
        var box = this.box = document.createElement('div');
        this.box.setAttribute('class',this.objs);
        var container = this.container = document.createElement('div');
        var footer = this.footer = document.createElement('div');
        this.setFooter();
        box.appendChild(container);
        box.appendChild(footer);
        document.body.appendChild(box);
    };
    //设置判断  @日期默认打开左右限定   仅限于限定状态处理
    DateBox.prototype.Detail = function(){
        if(this.num == 2){
            value = this.sizeOptis.mindata.split('-');
            return parseInt(value[1]);
        }else if(this.num == 1){
            var ids = parseInt(this.objs.replace('outDate',''));
            var value = document.getElementById("inDate"+ids).getAttribute("value");
            if(value == '' || value == ' '){
                value = stateTime.split('-');
            }else{
                value = value.split('-');
            }
            return parseInt(value[1]);
        }
    };
    //设置footer属性
    DateBox.prototype.setFooter = function(str){
        this.removeFootClick();
        var _this = this;
        if(str == 'today'){
            var dateTime = this.dateTime;
            this.footer.innerHTML = '今天:&nbsp;&nbsp;' + dateTime.getTodayStr();
            this.addFootClick(function(){
                dateTime.toToday();
                _this.inputTime();
            });
        }else{
            this.footer.innerHTML = '当前:&nbsp;&nbsp;' + str ||'';
        }
    };

    //添加footer点击事件
    DateBox.prototype.addFootClick = function(fn){
        this.removeFootClick();
        this.footClick = fn;
        // this.footer.addEventListener('click',this.footClick);
        this.footer.style.cursor = 'pointer';
    };
    //移除footer点击事件
    DateBox.prototype.removeFootClick = function(fn){
        this.footClick && this.footer.removeEventListener('click',this.footClick);
        this.footClick = null;
        this.footer.style.cursor = 'auto';
    };

    //初始化样式
    DateBox.prototype.initStyle = function(){

        this.hide();

        var boxStyle = this.opts.boxStyle;
        var bs = this.box.style;
        bs.border = parseInt(boxStyle.borderWidth) + 'px solid '+boxStyle.borderColor;
        bs.background  = boxStyle.backgroundColor;
        bs.position = 'absolute';
        bs.top = this.obj.getBoundingClientRect().bottom + 'px';

        bs.left = parseInt(this.obj.getBoundingClientRect().left-this.rightPosition) + 'px';

        var cs = this.container.style;

        var fs = this.footer.style;
        fs.height = parseInt(boxStyle.footerHeight) + 'px';
        fs.background = boxStyle.footerBackground;
        fs.color = boxStyle.footerFontColor;
        fs.lineHeight = parseInt(boxStyle.footerHeight) + 'px';
        fs.textAlign = 'center';
        fs.fontSize = parseInt(boxStyle.footerFontSize) + 'px';
        fs.borderTop = parseInt(boxStyle.footerSegLineWidth) + 'px solid '+boxStyle.footerSegLineColor;
    };
    //添加事件
    DateBox.prototype.addEL = function(){
        var _this = this;
        //获取焦点
        this.obj.addEventListener('focus',function(){
            if(_this.num == 1 || _this.num == 2){
                _this.getInfoName()
            }
            var _sel = _this.obj.getAttribute("data-bool");
            if(_sel != 1){
                _this.show();
            }
        });
        //失去焦点
        this.obj.addEventListener('blur',function(){
            _this.hide();
            _this.initCS();
        });
        //阻止日期插件的点击失去焦点事件
        document.addEventListener('mousedown',function(ev){
            var e = ev||event;
            var a = e.srcElement || e.target;
            if((_this.status == 'show')&&(_this.box == a||_this.obj == a||hasEle(_this.box,a))){
                e.preventDefault();
                return false;
            }
        });
    };
    DateBox.prototype.getInfoName = function(){   //input聚焦与否判断
        var _this = this;
        if(_this.num == 2){
            var ids = parseInt(_this.objs.replace('inDate',''));
            var value = document.getElementById("outDate"+ids).getAttribute("value");
            if(value == '' || value == ' '){
                _this.sizeOptis.maxDatas = _this.sizeOptis.maxdata;
            }else {
                _this.sizeOptis.maxDatas = value;
            }
            _this.sizeOptis.minDatas = _this.sizeOptis.mindata;
        }else if(_this.num == 1){
            var ids = parseInt(_this.objs.replace('outDate',''));
            var value = document.getElementById("inDate"+ids).getAttribute("value");
            if(value == '' || value == ' '){
                _this.sizeOptis.minDatas = _this.sizeOptis.mindata;
            }else {
                _this.sizeOptis.minDatas = value;
            }
            _this.sizeOptis.maxDatas = _this.sizeOptis.maxdata;
        }
        var ser = _this.sizeOptis.minDatas.split('-');
        _this.dateTime.month = parseInt(ser[1]);
        //执行公共各区域函数内容
        _this.addComponent();				//添加组成部分
        _this.initCS();						//初始化组建状态
        _this.timeChangeCallBack();			//添加时间更改后的回掉函数
    };
    //显示
    DateBox.prototype.show = function(){
        this.status = 'show';
        this.box.style.top = this.obj.getBoundingClientRect().bottom + 'px';
        this.box.style.left = parseInt(this.obj.getBoundingClientRect().left-this.rightPosition) + 'px';
        this.box.style.display = 'block';
    };
    //隐藏
    DateBox.prototype.hide = function(){
        this.status = 'hide';
        this.box.style.display = 'none';
        if(this.num != 3 ){
            $("."+this.objs).children().html('');
        }
    };
    //根据类型添加组建
    DateBox.prototype.addComponent = function(){
        var month,date,hour;
        date = true;
        if(date){
            this.dateComponent = new DateComponent(this,this.opts.dateComponentStyle,this.obj,this.sizeOptis);
        }
        //清浮动
        clearFix(this.container);
    };

    //初始化组建状态 initComponentsStatus 简化 initCS
    DateBox.prototype.initCS = function(){
        this.monthComponent&&this.monthComponent.hide();
        this.dateComponent&&this.dateComponent.hide();
        this.hourComponent&&this.hourComponent.hide();

        this.dateComponent.show();
        this.setFooter('today');
    };

    DateBox.prototype.setDateTime = function(opts){
        this.dateTime.setDateTime(opts);
        this.inputTime();
    };

    DateBox.prototype.timeChangeCallBack = function(){
        var _this = this;
        this.dateTime.setChangeCallBack(function(){
            _this.monthComponent&&_this.monthComponent.update(_this.dateTime);
            _this.dateComponent&&_this.dateComponent.update(_this.dateTime);
            _this.hourComponent&&_this.hourComponent.update(_this.dateTime);
        });
    };

    DateBox.prototype.inputTime = function(){
        this.obj.value = this.dateTime.toString(this.opts.type);
        this.obj.blur();
    };

    return DateBox;

});